package com.rj.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.Base64;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
 * @author YFX_RANLUBO
 */
@SuppressWarnings("unused")
public class GZipUtils {
    public static final int BUFFER = 1024;
    public static final String EXT = ".gz";
    private static final Logger log = LoggerFactory.getLogger(GZipUtils.class);

    /**
     * 数据压缩
     *
     * @param data
     * @return
     */
    public static byte[] compress(byte[] data) {
        byte[] output = null;
        try (ByteArrayInputStream bais = new ByteArrayInputStream(data);
             ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
            // 压缩
            compress(bais, baos);
            output = baos.toByteArray();
            baos.flush();
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return output;
    }

    /**
     * 文件压缩
     *
     * @param file
     * @throws Exception
     */
    public static void compress(File file) throws Exception {
        compress(file, true);
    }

    /**
     * 文件压缩
     *
     * @param file
     * @param delete 是否删除原始文件
     */
    public static void compress(File file, boolean delete) {
        try (FileInputStream fis = new FileInputStream(file);
             FileOutputStream fos = new FileOutputStream(file.getPath() + EXT)) {
            compress(fis, fos);
            fos.flush();
            if (delete) {
                boolean fal = file.delete();
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }

    /**
     * 数据压缩
     *
     * @param is
     * @param os
     * @throws Exception
     */
    public static void compress(InputStream is, OutputStream os) throws Exception {

        GZIPOutputStream gos = new GZIPOutputStream(os);

        int count;
        byte data[] = new byte[BUFFER];
        while ((count = is.read(data, 0, BUFFER)) != -1) {
            gos.write(data, 0, count);
        }

        gos.finish();

        gos.flush();
        gos.close();
    }

    /**
     * 文件压缩
     *
     * @param path
     * @throws Exception
     */
    public static void compress(String path) throws Exception {
        compress(path, true);
    }

    /**
     * 文件压缩
     *
     * @param path
     * @param delete 是否删除原始文件
     * @throws Exception
     */
    public static void compress(String path, boolean delete) throws Exception {
        File file = new File(path);
        compress(file, delete);
    }

    /**
     * 数据解压缩
     *
     * @param data
     * @return
     * @throws Exception
     */
    public static byte[] decompress(byte[] data) throws Exception {
        ByteArrayInputStream bais = new ByteArrayInputStream(data);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        // 解压缩
        decompress(bais, baos);
        data = baos.toByteArray();
        baos.flush();
        baos.close();
        bais.close();
        return data;
    }

    /**
     * 数据解压缩
     *
     * @param is
     * @param os
     * @throws Exception
     */
    public static void decompress(InputStream is, OutputStream os) throws Exception {
        GZIPInputStream gis = new GZIPInputStream(is);
        int count;
        byte data[] = new byte[BUFFER];
        while ((count = gis.read(data, 0, BUFFER)) != -1) {
            os.write(data, 0, count);
        }

        gis.close();
    }

    /**
     * 数据解码并解压
     *
     * @param base64Data
     * @return
     * @throws IOException
     */
    public static String decData(String base64Data) throws Exception {
        byte[] compressedData = Base64.getDecoder().decode(base64Data);
        byte[] decompressedData = decompress(compressedData);
        // 将解压后的数据转换成字符串（如果需要）
        String decompressedString = new String(decompressedData, "UTF-8");
        // 输出解压后的数据
        return decompressedString;
    }

}